package com.assumejson.circular_dependency;

import com.assumejson.circular_dependency.config.AppConfig;
import com.assumejson.circular_dependency.service.AService;
import com.assumejson.circular_dependency.service.BService;
import com.assumejson.circular_dependency.service.CService;
import com.assumejson.circular_dependency.service.TestBean;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ConfigurationClassPostProcessor;
import org.springframework.core.Conventions;

import java.util.concurrent.TimeUnit;

/**
 * @author by gang.Li 2022/4/8 14:10
 */
public class CircularStart {
	public static void main(String[] args) {
		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
//		CService cService = context.getBean("CService", CService.class);
//		cService.test();
		context.register(AppConfig.class);
//		AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.genericBeanDefinition().getBeanDefinition();
//		TestBean testBean1 = new TestBean();
//		beanDefinition.setAttribute(Conventions.getQualifiedAttributeName(
//						ConfigurationClassPostProcessor.class, "configurationClass"),
//				testBean1);
//		context.registerBeanDefinition("testBean1", beanDefinition);

		context.refresh();

		TestBean testBean = context.getBean("testBean", TestBean.class);
		testBean.test();

//		// 循环依赖 死锁复现
//		new Thread(() -> {
//			AService aService = context.getBean("AService", AService.class);
//		}).start();
//
//		try {
//			TimeUnit.SECONDS.sleep(3);
//		} catch (InterruptedException e) {
//			e.printStackTrace();
//		}
//
//		new Thread(() -> {
//			AService aService = context.getBean("AService", AService.class);
//			System.out.println(aService);
//		}).start();

	}
}
